package serviceTest;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import service.IEquipmentService;
import common.BasePersistenceTest;
import entity.Company;
import entity.Equipment;
import entity.Staff;
import entity.Equipment.STATE;

public class EquipmentServiceTest extends BasePersistenceTest {

	@Autowired
	private IEquipmentService equipmentService;

	@Test
	public void testEquipmentLogin() {
		Company company = new Company("fudan");

		getPersistenceManager().save(company);

		Equipment equipment = new Equipment("笔记本", company);

		getPersistenceManager().save(equipment);

		equipmentService.login(equipment);

		assertEquals(STATE.EQUIPMENTINSTORE, equipment.getState());

	}

	@Test
	public void testEquipmentBorrowing() {

		Company company = new Company("fudan");

		getPersistenceManager().save(company);
		Equipment equipment = new Equipment("笔记本", company);

		getPersistenceManager().save(equipment);
		Staff staff = new Staff(company);

		getPersistenceManager().save(staff);
		equipmentService.login(equipment);

		equipmentService.borrow(staff, equipment);

		assertEquals(STATE.EQUIPMENTBORROWED, equipment.getState());
	}

	@Test
	public void testEquipmentSuccessReturning() {

		Company com = new Company("teffffff");
		Staff staff = new Staff(com);
		Equipment eq = new Equipment("tetet", com);

		getPersistenceManager().save(com);
		getPersistenceManager().save(staff);
		getPersistenceManager().save(eq);

		equipmentService.login(eq);
		equipmentService.borrow(staff, eq);

		equipmentService.returnEquipment(staff, eq);

		assertEquals(entity.Equipment.STATE.EQUIPMENTINSTORE, eq.getState());
	}

	@Test
	public void testEquipmentFailedReturning() {

		Company com = new Company("teffff");

		Staff staff = new Staff(com);
		Staff staff2 = new Staff(com);

		Equipment eq = new Equipment("tete", com);

		getPersistenceManager().save(com);
		getPersistenceManager().save(staff);
		getPersistenceManager().save(staff2);
		getPersistenceManager().save(eq);

		equipmentService.login(eq);
		equipmentService.borrow(staff, eq);

		equipmentService.returnEquipment(staff2, eq);

		equipmentService.printEventLog(eq);

		assertEquals(entity.Equipment.STATE.EQUIPMENTBORROWED, eq.getState());
	}

	@Test
	public void testEquipmentBad() {
		Company com = new Company("tefffff");

		Staff staff = new Staff(com);

		Equipment eq = new Equipment("tetett", com);

		getPersistenceManager().save(com);
		getPersistenceManager().save(staff);
		getPersistenceManager().save(eq);

		equipmentService.login(eq);
		equipmentService.bad(eq);

		assertEquals(entity.Equipment.STATE.EQUIPMENTBAD, eq.getState());
	}
	
	@Test
	public void testGetEquipmentState(){
		Company com = new Company("x");
		getPersistenceManager().save(com);

		Equipment eq = new Equipment("eq", com);
		getPersistenceManager().save(eq);

		assertEquals("NEW", equipmentService.getEquipmentState(eq));
		
		equipmentService.login(eq);
		
		assertEquals("INSTORE", equipmentService.getEquipmentState(eq));
	
		Staff staff = new Staff(com);
		getPersistenceManager().save(staff);

		equipmentService.borrow(staff, eq);

		assertEquals("BORROWED", equipmentService.getEquipmentState(eq));
		
		equipmentService.returnEquipment(staff, eq);
		
		assertEquals("INSTORE", equipmentService.getEquipmentState(eq));
		
		equipmentService.bad(eq);
		
		assertEquals("BAD", equipmentService.getEquipmentState(eq));
		
	}
	
	@Test
	public void testGetAccountByEquipmentName(){

		Company com = new Company("xx");
		getPersistenceManager().save(com);

		Equipment eq = new Equipment("eq", com);
		getPersistenceManager().save(eq);

		assertEquals(com, equipmentService.getAccount(eq));
		
		equipmentService.login(eq);
		
		assertEquals(com, equipmentService.getAccount(eq));
	
		Staff staff = new Staff(com);
		getPersistenceManager().save(staff);
		
		equipmentService.borrow(staff, eq);

		assertEquals(staff, equipmentService.getAccount(eq));

	}

	@Test
	public void testGetEventLog(){

		Company com = new Company("x");
		getPersistenceManager().save(com);

		Equipment eq = new Equipment("eq", com);
		getPersistenceManager().save(eq);

		equipmentService.login(eq);
		
		Staff staff = new Staff(com);
		getPersistenceManager().save(staff);

		equipmentService.borrow(staff, eq);

		equipmentService.returnEquipment(staff, eq);
		
		equipmentService.bad(eq);
		
		assertEquals(4, equipmentService.getEventLog(eq).size());
	}
}
